package org.example.jiaxiao.mapper;

import org.apache.ibatis.annotations.*;
import org.example.jiaxiao.entity.Coach;
import org.example.jiaxiao.entity.Student;

import java.util.List;

@Mapper
public interface CoachMapper {
    
    @Select("SELECT * FROM coach")
    @Results({
        @Result(property = "coachId", column = "coach_id"),
        @Result(property = "applyNumber", column = "apply_number"),
        @Result(property = "courseId", column = "course_id"),
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "coachName", column = "coach_name"),
        @Result(property = "coachPhone", column = "coach_phone"),
        @Result(property = "affiliatedDrivingSchool", column = "affiliated_driving_school"),
        @Result(property = "atiin", column = "atiin")
    })
    List<Coach> findAll();
    
    @Select("SELECT * FROM coach WHERE coach_id = #{coachId}")
    @Results({
        @Result(property = "coachId", column = "coach_id"),
        @Result(property = "applyNumber", column = "apply_number"),
        @Result(property = "courseId", column = "course_id"),
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "coachName", column = "coach_name"),
        @Result(property = "coachPhone", column = "coach_phone"),
        @Result(property = "affiliatedDrivingSchool", column = "affiliated_driving_school"),
        @Result(property = "atiin", column = "atiin")
    })
    Coach findById(Integer coachId);
    
    @Insert("INSERT INTO coach (coach_id, apply_number, course_id, student_id, coach_name, coach_phone, affiliated_driving_school, atiin) " +
            "VALUES (#{coachId}, #{applyNumber}, #{courseId}, #{studentId}, #{coachName}, #{coachPhone}, #{affiliatedDrivingSchool}, #{atiin})")
    int insert(Coach coach);
    
    @Update("UPDATE coach SET coach_name = #{coachName}, coach_phone = #{coachPhone}, " +
            "affiliated_driving_school = #{affiliatedDrivingSchool}, atiin = #{atiin} " +
            "WHERE coach_id = #{coachId}")
    int update(Coach coach);
    
    @Delete("DELETE FROM coach WHERE coach_id = #{coachId}")
    int deleteById(Integer coachId);
    
    @Select("SELECT s.* FROM student s WHERE s.coach_id = #{coachId}")
    @Results({
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "coachId", column = "coach_id"),
        @Result(property = "accountNumber", column = "account_number"),
        @Result(property = "applyNumber", column = "apply_number"),
        @Result(property = "studentName", column = "student_name"),
        @Result(property = "cardId", column = "card_id"),
        @Result(property = "type", column = "type"),
        @Result(property = "studentPhone", column = "student_phone")
    })
    List<Student> findStudentsByCoachId(Integer coachId);
    
    @Select("SELECT MAX(coach_id) FROM coach")
    Integer findMaxId();
}
